package com.eascs.ant.data.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.bson.Document;
import org.springframework.stereotype.Service;

import com.eascs.ant.common.service.BaseSevice;
import com.eascs.ant.data.dao.MongoDbDao;
import com.eascs.ant.data.entity.phone.JdPhoneResult;
import com.eascs.ant.data.entity.phone.ZolPhoneResult;
import com.eascs.ant.data.entity.price.NcPriceResult;
import com.eascs.ant.data.platform.Platform;

@Service
public class PriceService  extends BaseSevice{

	@Inject
	MongoDbDao mongoDao;
	
	/**
	 * 默认查询价格，前20条
	 * @return
	 */
	public List<NcPriceResult> queryNcPrice(){
		String[] descendFields = new String[]{"日期"};
		List<Document> list = mongoDao.getData(Platform.NC_PRICE.getPlatform(),20 , descendFields);
		List<NcPriceResult> nps = new LinkedList<>();
		for(Document doc : list){
			NcPriceResult np = new NcPriceResult();
			np.setDate(doc.getDate("日期"))
			  .setMarket(doc.getString("市场"))
			  .setPrice(doc.getString("价格"))
			  .setProduct(doc.getString("产品"))
			  .setTrend(doc.getString("走势"));
			nps.add(np);
		}
		return nps;
	}
	
	/**
	 * 根据日期查新农商网价格行情
	 * @param queryDate
	 * @return
	 * @throws ParseException
	 */
	public List<NcPriceResult> queryNcPrice(String queryDate) throws ParseException{
		Map<String, Object> filterMap = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		filterMap.put("日期", sdf.parse(queryDate));
		
		List<Document> list = mongoDao.getData(Platform.NC_PRICE.getPlatform() , filterMap);
		List<NcPriceResult> nps = new LinkedList<>();
		
		for(Document doc : list){
			NcPriceResult np = new NcPriceResult();
			np.setDate(doc.getDate("日期"))
			  .setMarket(doc.getString("市场"))
			  .setPrice(doc.getString("价格"))
			  .setProduct(doc.getString("产品"))
			  .setTrend(doc.getString("走势"));
			nps.add(np);
		}
		return nps;
	}
	
	/**
	 * 根据日期查中关村手机信息
	 * @param queryDate
	 * @return
	 * @throws ParseException
	 */
	public List<ZolPhoneResult> queryZolPhone(String queryDate) throws ParseException{
		Map<String, Object> filterMap = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		filterMap.put("crawler_time", sdf.parse(queryDate));
		List<ZolPhoneResult> zprs = new LinkedList<>();
		List<Document> list = mongoDao.getData(Platform.ZOL_PHONE.getPlatform() , filterMap);
		for(Document doc : list){
			ZolPhoneResult zpr = new ZolPhoneResult();
			//自定义字段部分
			zpr.setName(doc.getString("name"))
			   .setBrand(doc.getString("brand"))
			   .setMernum(doc.getString("mernum"))
			   .setCrawler_time(doc.getDate("crawler_time"))
			   .setUrl(doc.getString("url"));
			if(doc.containsKey("type")){
				zpr.setType(doc.getString("type"));
			}else{
				zpr.setPrice(doc.getString("price"))
				   .setPublishTime(doc.getString("date"))
				   .setLow_price_url(doc.getString("low_price_url"))
				   .setMernum(doc.getString("mernum"));
			}
			zpr.setCankaobaojia(doc.getString("参考报价"))
			   .setHouzhishexiangtou(doc.getString("后置摄像头"))
			   .setZhupingchicun(doc.getString("主屏尺寸"))
			   .setDianchirongliang(doc.getString("电池容量"))
			   .setRomRongliang(doc.getString("ROM容量"))
			   .setCpuXinghao(doc.getString("CPU型号"))
			   .setForGnetType(doc.getString("4G网络"))
			   .setCpuPinlv(doc.getString("CPU频率"))
			   .setCaozuoxitong(doc.getString("操作系统"))
			   .setRamRongliang(doc.getString("RAM容量"))
			   .setShangjiabaojia(doc.getString("商家报价"))
			   .setZaishoudianshang_jingdong(doc.getString("在售电商报价之京东"))
			   .setZaishoudianshang_tmall(doc.getString("在售电商报价之天猫商城"))
			   .setZaishoudianshang_zol(doc.getString("在售电商报价之ZOL商城"));
			zprs.add(zpr);
		}
		return zprs;
	}	
	
	/**
	 * 查京东手机价格信息
	 * @param queryDate
	 * @return
	 * @throws ParseException
	 */
	public List<JdPhoneResult> queryJdPhone(String queryDate) throws ParseException{
		//由于京东手机数据是python代码datetime入mongodb,入库时间及可视化工具展示的都是东八区时间，
		//但java mongodb API 操作时间（包括入库，根据时间查询）都会将date转为格林威治时间,所以这里要将京东查询时间往后延8小时
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> filterMap = new HashMap<String, Object>();
		long queryTime = sdf.parse(queryDate).getTime() + 1000*3600*8;
		filterMap.put("crawler_time", new Date(queryTime));
		List<JdPhoneResult> jprs = new LinkedList<>();
		List<Document> list = mongoDao.getData(Platform.JINGDONG_PHONE.getPlatform() , filterMap);
		for(Document doc : list){
			JdPhoneResult jpr = new JdPhoneResult();
			jpr.setName(doc.getString("name"))
			   .setPrice(doc.getString("price"))
			   .setCommit(doc.getString("commit"))
			   .setShop(doc.getString("shop"))
			   .setIcon(doc.getString("icon"))
			   .setLogo(doc.getString("logo"))
			   .setUrl(doc.getString("url"))
			   .setCrawler_time(doc.getDate("crawler_time"));
			jprs.add(jpr);
		}
		return jprs;
	}
	
}
